﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//-----------------------------------
using Controller;
using BusinessModel;
using ManageStore.Config;
using System.Data.SqlClient;
using ManageStore.IViews;
using BaseUtilities;

namespace ManageStore.Forms
{
    public partial class FrmUnit : BaseForm, IUnitView
    {
        public FrmUnit()
        {
            InitializeComponent();

            UnitCtr = new UnitCtr(this);
            unitCtrBindingSource.DataSource = UnitCtr;
            EvtNew += UnitCtr.MtdNew;
            EvtEdit += UnitCtr.MtdEdit;
            EvtDelete += UnitCtr.MtdDelete;
            EvtSave += UnitCtr.MtdSave;
        }
        private void FrmUnit_Load(object sender, EventArgs e)
        {
            dgvUnit.AutoGenerateColumns = false;
            dgvUnit.DataSource = UnitCtr.LstUnit;
        }

        #region Properties
        // UnitCtr.
        private UnitCtr _UnitCtr;
        public UnitCtr UnitCtr
        {
            get { return _UnitCtr; }
            set { _UnitCtr = value; }
        }
        //----------------------------------------
        public event EventHandler EvtSaved;
        public bool IsDialog { get; set; }
        public string UnitName
        {
            get { return txtName.Text; }
            set { txtName.Text = value; }
        }

        public bool IsLoaded { get; set; }
        #endregion Properties

        // Before.
        //----------------------------------------
        public override bool BeforeHandler(string pEventName, params object[] pParams)
        {
            bool result = true;
            switch (pEventName)
            {
                case "EvtDelete":
                    if (MessageBox.Show("Bạn có chắc muốn xóa không?", "Cảnh báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Cancel)
                    {
                        result = false;
                    }
                    break;
            }
            return result;
        }
        // Error.
        //----------------------------------------
        public override bool ErrorHandler(string pEventName, params object[] pParams)
        {
            bool result = true;
            switch (pEventName)
            {
                case "EvtDelete":
                    if (pParams.Count() == 1)
                    {
                        ResultModel oResultModel = pParams[0] as ResultModel;
                        if (oResultModel.ErrorNumber == ManageStoreConfig.ErrorForeignKey)
                        {
                            MessageBox.Show("Đơn vị tính này đã được dùng. Không thể xóa được!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    break;
                case "EvtSave":
                    string sError = "Không thể lưu được.";
                    if (pParams.Count() == 1)
                    {
                        Dictionary<string, Dictionary<string, string>> lstError = pParams[0] as Dictionary<string, Dictionary<string, string>>;
                        if (lstError != null && lstError.Count > 0)
                        {
                            sError = "Dữ liệu nhập vào không hợp lệ.";
                        }
                    }
                    MessageBox.Show(sError, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
            }
            return result;
        }
        // Completed.
        //----------------------------------------
        public override bool CompletedHandler(string pEventName, params object[] pParams)
        {
            bool result = true;
            switch (pEventName)
            {
                case "EvtNew":
                    txtName.Focus();
                    errorProvider1.Clear();
                    break;
                case "EvtEdit":
                    txtName.SelectAll();
                    txtName.Focus();
                    errorProvider1.Clear();
                    break;
                case "EvtSave":
                    int iUnitId = 0;
                    int.TryParse(pParams[0].ToString(), out iUnitId);
                    if (EvtSaved != null)
                    {
                        EvtSaved(iUnitId, null);
                        if (IsDialog)
                            this.Close();
                    }
                    OnEvtNew();
                    break;
                case "EvtDelete":
                    OnEvtNew();
                    break;
            }
            return result;
        }
        // Validation.
        //----------------------------------------
        public override bool ValidationHandler(string pColumnName, params object[] pParams)
        {
            bool result = true;
            if (!string.IsNullOrWhiteSpace(pColumnName))
            {
                // Get Error control.
                Control ctl = null;
                switch (pColumnName)
                {
                    case "UnitName":
                        ctl = txtName;
                        break;
                }
                // Get Error message.
                string sError = null;
                pParams.ToList().ForEach((item) =>
                {
                    Dictionary<string, string> dicEle = item as Dictionary<string, string>;
                    if (dicEle != null)
                    {
                        foreach (string s in dicEle.Keys)
                        {
                            sError += dicEle[s] + "\r\n";
                        }
                    }
                });
                // Set Control Error.
                if (ctl != null)
                {
                    errorProvider1.SetError(ctl, sError);
                }
            }
            return result;
        }

        public void ReloadData()
        {
            //dgvUnit.DataSource = null;
            dgvUnit.DataSource = UnitCtr.LstUnit;
        }
        #region Dotnet Methods
        //----------------------------------------
        private void btnAdd_Click(object sender, EventArgs e)
        {
            OnEvtNew();
        }
        //----------------------------------------
        private void btnSave_Click(object sender, EventArgs e)
        {
            OnEvtSave();
        }
        //----------------------------------------
        private void btnDelete_Click(object sender, EventArgs e)
        {
            OnEvtDelete();
        }
        //----------------------------------------
        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //----------------------------------------
        private void dgvUnit_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            dgvUnit.ClearSelection();
            //OnEvtNew();
        }
        //----------------------------------------
        private void dgvUnit_SelectionChanged(object sender, EventArgs e)
        {
            if (IsLoaded && dgvUnit.SelectedRows.Count > 0)
            {
                OnEvtEdit(dgvUnit.SelectedRows[0].DataBoundItem);
            }
        }
        //----------------------------------------
        private void FrmUnit_Activated(object sender, EventArgs e)
        {
            if (!IsLoaded)
            {
                IsLoaded = true;
            }
        }
        #endregion Dotnet Methods
    }
}
